/*
 * 上海远境文化传媒有限公司 版权所有
 */
package models.base;

import ext.jpa.SnowflakeModel;
import facade.base.CorpFacade;
import helper.GlobalConfig;
import models.enums.ClientType;
import org.hibernate.annotations.Index;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import java.util.Date;
import java.util.List;

/**
 * 实体日志: 记录实体对象发生的操作日志.
 */
@Entity
@Table(name = "entity_logs", schema = GlobalConfig.MIGAO_DB_SCHEMA)
@org.hibernate.annotations.Table(
        appliesTo = "entity_logs",
        indexes = {
                @Index(name = "idx_entity_logs_key", columnNames = {"entity_id"}),
                @Index(name = "idx_entity_logs_operid", columnNames = {"operator_id"})
        }
)
public class EntityLog extends SnowflakeModel {

    /**
     * 所属公司.
     */
    @ManyToOne
    @JoinColumn(name = "corp_id")
    public Corp corp;

    /**
     * 业务对象ID.
     */
    @Column(name = "entity_id", columnDefinition = "char(22)")
    public String entityId;

    /**
     * 操作用户ID.
     */
    @Column(name = "operator_id", columnDefinition = "char(22)")
    public String operatorId;

    /**
     * 操作用户姓名.
     */
    @Column(name = "operator_name", length = 30)
    public String operatorName;

    /**
     * 客户端类型.
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "client_type", length = 20)
    public ClientType clientType;

    @Column(name = "log_message", length = 1024)
    public String logMessage;

    /**
     * 日志ID.
     */
    @Column(name = "ttid", length = 20)
    public String ttid;

    /**
     * 请求 HOST
     */
    @Column(name = "request_host", length = 50)
    public String requestHost;

    /**
     * 创建时间.
     */
    @Column(name = "created_at")
    public Date createdAt;

    /**
     * 创建日志.
     */
    public static EntityLog createLog(Corp corp, String entityId, String operatorId, String operatorName, ClientType clientType, String logMessage, String ttid, String requestHost) {
        EntityLog entityLog = new EntityLog();
        entityLog.corp = corp;
        if (corp == null && CorpFacade.isPrivateInstallVersion()) {
            entityLog.corp = Corp.findFirst();
        }
        entityLog.entityId = entityId;
        entityLog.operatorId = operatorId;
        entityLog.operatorName = operatorName;
        entityLog.clientType = clientType;
        entityLog.logMessage = logMessage;
        entityLog.createdAt = new Date();

        // 日志点.
        if (ttid != null) {
            entityLog.ttid = String.valueOf(ttid);
        }

        // 请求地址.
        if (requestHost != null) {
            entityLog.requestHost = requestHost;
        }

        entityLog.save();
        return entityLog;
    }

    public static EntityLog createLog(Corp corp, String entityId, AdminUser user, ClientType clientType, String logMessage, String ttid, String requestHost) {
        if (user == null) {
            return null;
        }
        return createLog(corp, entityId, user.id, user.name, clientType, logMessage, ttid, requestHost);
    }

    public static List<EntityLog> findByEntityId(Corp corp, String entityId) {
        return EntityLog.find("corp=?1 and entityId=?2 order by id desc", corp, entityId).fetch(100);
    }

    public static List<EntityLog> findByEntityIdAsc(Corp corp, String entityId) {
        return EntityLog.find("corp=?1 and entityId=?2 order by id asc", corp, entityId).fetch(100);
    }

    public static List<EntityLog> findByEntityIdAsc(String entityId) {
        return EntityLog.find("entityId=?1 order by id asc", entityId).fetch(100);
    }
}
